<?php
/**
 * User Model.
 * @package    spf.modules.users
 * @author     Simon Downes <simon@simondownes.co.uk>
 * @copyright  Copyright &copy; 2007, Simon Downes 
 * @license    http://www.opensource.org/licenses/mit-license.php
 */

/**
 * User Model.
 *
 * @package    spf.modules.users
 * @author     Simon Downes <simon@simondownes.co.uk>
 * @copyright  Copyright &copy; 2007, Simon Downes 
 * @license    http://www.opensource.org/licenses/mit-license.php
 */
final class User extends SPF_Model {
   
   /**
    * Constructor.
    *
    * @return  void
    */
   public function __construct() {

      /* construct the parent object */
      parent::__construct('users');
      
      $this->add_validation('user_name', true, 'alphanumeric', '_', 5);
      $this->add_validation('display_name', true, 'alphanumeric', ' ');
      $this->add_validation('email', true, 'email', ' ');
      
   } // __construct

   /**
    * Destructor.
    *
    * @return  void
    */
   public function __destruct() {
      parent::__destruct();
   }
   
   public function before_update( $new ) {
   
      // password validation...
      if( strlen($this->password) != SPF::HASH_LEN ) {
         $this->password = SPF::hash($this->password);
      }
      
   } // before_update
   
   public function validate() {
      
      // default validation
      parent::validate();
      
      // email address can only exist once
      
      // password validation...
      if( strlen($this->password) != SPF::HASH_LEN ) {
         
         if( !isset($this->confirm_pw) )
            $this->confirm_pw = NULL;
         
         if( !isset($this->errors['password']) && !$this->password ) {
            $this->errors['password'] = 'Required.';
         }
         
         if( !isset($this->errors['password']) && ($this->password != $this->confirm_pw) ) {
            $this->errors['confirm_pw'] = 'Passwords do not match.';
         }
         
      }
      
   } // signin
   
   public static function signin( $user_name, $password ) {
      
      $sql = "SELECT user_status
                FROM users
               WHERE user_name = '$user_name'
                 AND password  = '$password'";
      return SPF::$db->get_one($sql);
      
   } // signin
   
   public static function activate( $user_name, $password ) {
      
      $sql = "UPDATE users
                 SET user_status  = 'ACTIVE'
               WHERE user_name    = '$user_name'
                 AND password     = '$password'
                 AND user_status != 'DELETED'";
      SPF::$db->execute($sql);
      
      return SPF::$db->affected_rows() == 1;

   } // activate
   
   public static function browse_data() {
      
      $sql = "SELECT user_name,
                     display_name,
                     email,
                     user_status
                FROM users
            ORDER BY user_status,
                     user_name";
      return SPF::$db->get_All($sql);
   
   } // browse_data
   
   public static function get_count_by_status() {
      
      $sql = "SELECT user_status,
                     COUNT(*) AS user_count
                FROM users
            GROUP BY user_status";
      $rs = SPF::$db->get_all($sql);
      
      $data = array();
      foreach( $rs as $rec ) {
         $data[$rec['user_status']] = $rec['user_count'];
      }
      
      $enums = SPF::$db->get_enums('users', 'user_status');
      foreach( $enums as $status ) {
         if( !isset($data[$status]) )
            $data[$status] = 0;
      }
      
      return $data;
      
   } // get_count_by_status
   
   public static function lookup_status_options() {
      
      return SPF::$db->get_enums('users', 'user_status');
      
   } // lookup_status_options
   
	/**
	 * Returns an array containing the entries from a lookup table
	 *
	 * @param   integer   $current_id   the current_id to retrieve.
	 * @param   boolean   $inactive     should inactive records be included?
    * @return  array
	 */
   public static function lookup_options( $current_user = '', $status = '' ) {
      
      $sql = 'SELECT user_name, display_name
               FROM users';
      
      if( !$status )
         $sql .= " WHERE user_status = 'ACTIVE'
                      OR user_name = '$current_user'";
      
      $sql .= ' ORDER BY display_name';
      
      $rs = SPF::$db->get_all($sql);
      
      $arr = array();
      foreach( $rs as &$row ) {
         $arr[ $row['user_name'] ] = $row['display_name'];
      }
      
      return $arr;
      
   } // lookup_options

} // User

?>
